<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{
    
    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;
    protected $noNeedLogin = [];
    protected $noNeedRight = ['ajaxconfirmorder','ajaxshippingorder'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Order');
        $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("deletedList", $this->model->getDeletedList());
        $this->view->assign("shippingStatusList", $this->model->getShippingStatusList());
        $this->view->assign("orderPromTypeList", $this->model->getOrderPromTypeList());
        $this->view->assign("goodsNumList", $this->model->getGoodsNumList());
    }



    /**
     * 查看
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);

        if (!$row)
            $this->error(__('No Results were found'));

        $data = $row->toArray();

        $hiddenarr = [
            'pay_time','order_prom_type','user_id','goods_id','first_user_id,deleted'
        ];
        foreach ($data as $k => $v){
            if(in_array($k,$hiddenarr)){
                unset($data[$k]);
            }
        }


        $this->view->assign('shippingList',$this->model->getshippingList());
        $this->view->assign("row",$data);
        return $this->view->fetch();
    }

    /**
     * ajax确定订单
     */
    public function ajaxConfirmOrder(){
        $order_id = $this->request->request('order_id');
        $order = $this->model->get($order_id);
        if (!$order)
            $this->error(__('No Results were found'));

        if($order['pay_status'] != 1 || $order['order_status'] != 0){
            $this->error('此订单支付状态不允许确定');
        }

        $order->order_status = 1;
        $res = $order->save();

        if($res !== false){
            $this->success('订单确认成功');

        }else{
            $this->error('订单确认失败');
        }
    }

    /**
     * 设置单号，发货
     */
    public function ajaxShippingOrder(){
        $order_id = $this->request->request('order_id');
        $shipping_name = $this->request->request('shipping_name');
        $shipping_code = $this->request->request('shipping_code');
        $order = $this->model->get($order_id);
        if (!$order)
            $this->error(__('No Results were found'));

        if($order['pay_status'] != 1 || $order['order_status'] != 1){
            $this->error('此订单支付状态不允许确定');
        }

        $order->shipping_status = 1;
        $order->shipping_name = $shipping_name;
        $order->shipping_code = $shipping_code;
        $res = $order->save();

        if($res !== false){
            $this->success('订单确认发货成功');

        }else{
            $this->error('订单确认发货失败');
        }

    }
    

    

}
